package com.sihuatech.coreagent.until;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil extends FileUtils {
	private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

	/**
	 * 创建或更新文件
	 * 
	 * @param filePath 文件路径
	 * @param sources  写入文件的字符串
	 */
	public static void createOrUpdateFile(File file, String sources) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			fw = new FileWriter(file, true);
			bw = new BufferedWriter(fw);
			bw.write(sources);
			bw.flush();
		} catch (IOException e) {
			logger.error("写文件时异常", e);
		} finally {
			try {
				bw.close();
				fw.close();
			} catch (Exception e) {
				logger.error("关闭流时异常", e);
			}
		}
	}
	
	/**
	 * 创建文件
	 * @param path
	 * @return
	 */
	public static void createFile(String path) {
		try {
			File file = new File(path);
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
		} catch (Exception e) {
			logger.error("创建文件异常", e);
		}
	}
	
}
